package com.winexpress.session.account;

import com.winexpress.entity.Customer;
import com.winexpress.entity.Subscriber;
import com.winexpress.entity.User;
import com.winexpress.helper.AESEncryptor;
import com.winexpress.helper.Config;
import com.winexpress.helper.Encryptor;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

@Stateless
@LocalBean
public class CustomerSessionBean {

    @PersistenceContext
    private EntityManager em;

    public Long registerNewCustomer(Customer customer) throws Exception {
        em.persist(customer);
        return customer.getId();
    }

    public Long addNewSubscriber(String email) {
        Subscriber s = new Subscriber();
        s.setEmail(email);
        em.persist(s);
        return s.getId();
    }

    public Long registerFacebookCustomerAccount(String facebookId, String firstName, String LastName) throws Exception {
        Customer customer = new Customer();
        customer.setFacebookID(facebookId);
        customer.setFirstName(firstName);
        customer.setLastName(LastName);
        customer.setActivated(true);
        customer.setAdminUser(false);
        em.persist(customer);
        return customer.getId();
    }

    public Long registerGoogleCustomerAccount(String googleId, String firstName, String LastName, String email) throws Exception {
        Customer customer = new Customer();
        customer.setGooogleID(googleId);
        customer.setFirstName(firstName);
        customer.setLastName(LastName);
        customer.setEmail(email);
        customer.setActivated(true);
        customer.setAdminUser(false);
        em.persist(customer);
        return customer.getId();
    }

    public Customer getCustomerByFacebookId(String id) throws Exception {
        String strQuery = "SELECT d FROM Customer d WHERE d.facebookID = :inID";
        Query query = em.createQuery(strQuery);
        query.setParameter("inID", id);
        List<Customer> list = (List<Customer>) query.getResultList();
        if (!list.isEmpty()) {
            return list.get(0);
        }
        return null;
    }

    public Customer getCustomerByGoogleId(String id) throws Exception {
        String strQuery = "SELECT d FROM Customer d WHERE d.gooogleID = :inID";
        Query query = em.createQuery(strQuery);
        query.setParameter("inID", id);
        List<Customer> list = (List<Customer>) query.getResultList();
        if (!list.isEmpty()) {
            return list.get(0);
        }
        return null;
    }

    public List<String> getCustomerSubscribeEmails() throws Exception {
        String strQuery = "SELECT c FROM Customer c WHERE c.subscribed = :isSubscribed";
        Query query = em.createQuery(strQuery);
        query.setParameter("isSubscribed", true);
        List<Customer> list = (List<Customer>) query.getResultList();
        List<String> emails = new ArrayList<>();
        if (!list.isEmpty()) {
            Iterator it = list.iterator();
            while (it.hasNext()) {
                Customer c = (Customer) it.next();
                emails.add(c.getEmail());
            }
        }

        return emails;
    }

    public String loginCustomer(Customer c) throws Exception {
        String strQuery = "SELECT d FROM Customer d WHERE d.email = :inEmail AND d.password = :inPassword";
        Query query = em.createQuery(strQuery);
        query.setParameter("inEmail", AESEncryptor.encrypt(Config.seed, c.getEmail()));
        query.setParameter("inPassword",c.getPassword());
        List<Customer> list = (List<Customer>) query.getResultList();
        if (!list.isEmpty()) {
            Customer customer = list.get(0);
            if ((!customer.isAdminUser()) && customer.isActivated()) {
                return customer.getId().toString();
            }
        }
        return null;
    }

    public Customer getCustomerById(Long id) {
        return em.find(Customer.class, id);
    }

    public Customer getCustomerByEmail(String email) throws Exception {
        String strQuery = "SELECT d FROM Customer d WHERE d.email = :inEmail";
        Query query = em.createQuery(strQuery);
        query.setParameter("inEmail", AESEncryptor.encrypt(Config.seed, email));
        List<Customer> list = (List<Customer>) query.getResultList();
        if (list.isEmpty()) {
            return null;
        }
        return list.get(0);
    }

    public Customer updateCustomerParticular(Customer customer) throws Exception {
        em.merge(customer);
        em.flush();
        return customer;
    }

    public boolean deleteCustomerAccount(Long id) throws Exception {
        Customer customer = em.find(Customer.class, id);
        if (customer == null) {
            return false;
        }
        customer.setActivated(false);
        Random randomGenerator = new Random();
        customer.setActivationCode(String.valueOf(randomGenerator.nextLong()));
        return true;
    }
}
